// pages/index/detail.js
const app = getApp();
const WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    all: {},
    host: '',
    article: '<p>暂未添加</p>',
    deg: false,
    degshow: true,
    scimg:'xiangqing_shoucang_nor.png',
    sc_yes: 'xiangqing_shoucang_sel.png',
    text: '收藏',
    current: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      task_id
    } = options;

    let url = 'v2.task/replyTask';
    app.http.$ajax({
      url,
      data: {
        task_id: task_id
      }
    }, {
      success: (data) => {
        var that = this;
        var taskData = data.data;
        WxParse.wxParse('article', 'html', taskData.task.content, that, 5);
          if (taskData.favorite == 1) {
            that.data.scimg = that.data.sc_yes;
            that.data.text = '已收藏';
        }     
        app.processImg(taskData.ortherTask);
        let current = taskData.is_receive != 0 ? taskData.is_receive.type : 0;
        that.setData({
          taskData: taskData,
          task_id:task_id,
          scimg: that.data.scimg,
          text: that.data.text,
          current: current
        });
        //填充内容后，获取内容高度
        var obj = wx.createSelectorQuery();
          obj.select('#task-introduce').boundingClientRect(function(rect){
              let height = rect.height;
              wx.getSystemInfo({
                success:function (res) {
                    let rpxheight = (height * (750 / res.windowWidth));
                    if(rpxheight <= 320) {
                        that.setData({
                            degshow: false
                        })
                    }
                }
              }) 
          }).exec()
      }
    })
  },
  favorite(e) {
    var {
      task_id
    } = e.currentTarget.dataset;
    app.http.$ajax({
      url: 'v2.task/collect',
      data: {
        task_id: task_id
      },
      loading: '收藏中...'
    }, {
        success: (data) => {
          if(data.code==1){
            this.setData({
              scimg: 'xiangqing_shoucang_sel.png',
              text:'已收藏'
            })
          }   
        }
    })
  },
  reply(e) {
    let that = this;
    let { task_id} = e.currentTarget.dataset;
    app.http.$ajax({ url: 'v2.task/replay', data: { task_id: task_id },loading: '申请中...' }, {
      success: (data) => {
        wx.showToast({
          title: data.msg,
          duration: 200,
          icon: 'none',
        })
        if (data.code == 1) {
          setTimeout(function () {
            that.onLoad({task_id: task_id});
          }, 1000)
        }
      }
    })
  },
  more: function() {
    this.setData({
      deg: !this.data.deg
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})